package com.zecan.blog.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.UUID;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.OSSObject;
import com.zecan.blog.entity.AliOss;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * @ClassName AliOssUtils
 * @Description
 * @date 2022/11/3 14:24
 * @Version 1.0
 */
@Component
public class AliOssUtils {


    /**
     * oss文件上传
     *
     * @param inputStream 输入
     * @param module   存放路径
     * @param originalFilename 文件
     * @return
     */
    public String upload(InputStream inputStream, String module, String originalFilename) {

        // 读取配置
        String endpoint = AliOss.END_POINT;
        String keyid = AliOss.ACCESS_KEY_ID;
        String keysecret =AliOss.ACCESS_KEY_SECRET;
        String bucketname = AliOss.BUCKET_NAME;

        //判断oss实例是否存在：如果不存在则创建，如果存在则获取
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
        if (!ossClient.doesBucketExist(bucketname)) {
            //创建bucket
            ossClient.createBucket(bucketname);
            //设置oss实例的访问权限：公共读
            ossClient.setBucketAcl(bucketname, CannedAccessControlList.PublicRead);
        }

        //文件名：uuid.扩展名
        String fileName = UUID.randomUUID().toString();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String key = module +  "/" + fileName + fileExtension;

        //文件上传至阿里云
        ossClient.putObject(bucketname, key, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();

        //返回url地址
        return "https://" + bucketname + "." + endpoint + "/" + key;
    }


    //    public byte[] getImage(@PathVariable String imageName) {
//        // EndPoint以上海为例
//        String endPoint = "oss-cn-shanghai.aliyuncs.com";
//        // 阿里云API秘钥
//        String accessKeyId = "<your access key id>";
//        String accessKeySecret = "<your access key secret>";
//        String bucketName = "<bucket name>";
//        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
//        OSSObject image = client.getObject(bucketName, imageName);
//        InputStream content = image.getObjectContent();
//        if (content != null) {
//            try {
//                // 设置一个足够大的buffer用于存储图片的比特数据
//                int length = 1920 * 1260 * 3;
//                byte[] buf = new byte[length];
//                int size = 0;
//                int temp;
//                while ((temp = content.read()) != -1) {
//                    buf[size] = (byte) temp;
//                    size++;
//                }
//                content.close();
//                // 对缓冲区进行裁剪后，将图片以字节数组的形式返回
//                return Arrays.copyOf(buf, size);
//            } catch (IOException e) {
//                System.out.println("exception");
//                return null;
//            }
//        }
//        client.shutdown();
//        return null;
//    }
    public OSSObject getImage(String imageName, AliOss oss) {
        // EndPoint以上海为例
        String endPoint =oss.getEndPoint();
        // 阿里云API秘钥
        String accessKeyId = oss.getAccessKey();
        String accessKeySecret = oss.getAccessKey();
        String bucketName =oss.getBucketName();
        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        OSSObject image = client.getObject(bucketName, imageName);
        return image;

    }


}
